<?php

namespace app\model;

use think\Model;

// 订单表
class Orders extends Model
{
    // Define the table name
    protected $name = 'orders';
    public $timestamps = false;
    
    // 订单状态常量
    const STATUS_CREATED = 'created';     // 已创建
    const STATUS_UNPAID = 'unpaid';       // 待支付
    const STATUS_PAID = 'paid';           // 已支付
    const STATUS_SHIPPED = 'shipped';     // 已发货
    const STATUS_COMPLETED = 'completed'; // 已完成
    const STATUS_CANCELLED = 'cancelled'; // 已取消
    
    // 支付状态常量
    const PAY_STATUS_UNPAID = 0;  // 未支付
    const PAY_STATUS_PAID = 1;    // 支付成功
    const PAY_STATUS_FAILED = 2;  // 支付失败
    
    // 订单类型常量
    const TYPE_MALL = 'mall';
    const TYPE_CHECKOUT = 'checkout';
    
    // 订单产品关联
    public function orderProducts()
    {
        return $this->hasMany(OrderProducts::class, 'order_id', 'id');
    }
    
    // 用户关联
    public function customer()
    {
        return $this->hasOne(User::class, 'id', 'customer_id');
    }
    
    // 收货地址关联
    public function shippingAddress()
    {
        return $this->hasOne(UserAddresses::class, 'id', 'shipping_address_id');
    }

    /**
     * 获取订单状态文本描述
     * @param string $status
     * @return string
     */
    public static function getOrderStatusText($status)
    {
        $statusMap = [
            Orders::STATUS_CREATED => '已创建',
            Orders::STATUS_UNPAID => '待支付',
            Orders::STATUS_PAID => '已支付',
            Orders::STATUS_SHIPPED => '已发货',
            Orders::STATUS_COMPLETED => '已完成',
            Orders::STATUS_CANCELLED => '已取消'
        ];
        
        return $statusMap[$status] ?? '未知状态';
    }
}